package cn.cuilan.other.xlsx;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Pair;
import cn.hutool.core.text.csv.*;
import cn.hutool.core.util.CharsetUtil;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zhangyan
 * @since 2023/1/9
 */
public class Calc {

    private static final String csvFile = "/Users/zhangyan/Downloads/蓬莱2013-2022.csv";

    // P1-4  120.13727591666667  38.432009
    // p1-3  120.142893          38.42757683333333
    // p1-2  120.14850927777778  38.423144944444445
    // p1-1  120.154125          38.418712222222226
    private static Pair<BigDecimal, BigDecimal> point4 = new Pair<>(new BigDecimal("120.13727591666667"), new BigDecimal("38.432009"));
    private static Pair<BigDecimal, BigDecimal> point3 = new Pair<>(new BigDecimal("120.142893"), new BigDecimal("38.42757683333333"));
    private static Pair<BigDecimal, BigDecimal> point2 = new Pair<>(new BigDecimal("120.14850927777778"), new BigDecimal("38.423144944444445"));
    private static Pair<BigDecimal, BigDecimal> point1 = new Pair<>(new BigDecimal("120.154125"), new BigDecimal("38.418712222222226"));

    public static void main(String[] args) {
        List<CsvRow> distance1 = new ArrayList<>();
        List<CsvRow> distance2 = new ArrayList<>();
        List<CsvRow> distance3 = new ArrayList<>();
        List<CsvRow> distance4 = new ArrayList<>();

        CsvReader reader = CsvUtil.getReader();
        // 从文件中读取CSV数据
        CsvData data = reader.read(FileUtil.file(csvFile));
        List<CsvRow> rows = data.getRows();
        CsvRow title = rows.get(0);
        distance1.add(title);
        distance2.add(title);
        distance3.add(title);
        distance4.add(title);

        for (int i = 1; i < rows.size(); i++) {
            List<String> rawList = rows.get(i).getRawList();
            double lat = new BigDecimal(rawList.get(0)).doubleValue();
            double lon = new BigDecimal(rawList.get(1)).doubleValue();

            double d1 = GeoUtil.distance(lon, lat, point1.getKey().doubleValue(), point1.getValue().doubleValue());
            if (d1 < 10000) {
                distance1.add(rows.get(i));
            }

            double d2 = GeoUtil.distance(lon, lat, point2.getKey().doubleValue(), point2.getValue().doubleValue());
            if (d2 < 10000) {
                distance2.add(rows.get(i));
            }

            double d3 = GeoUtil.distance(lon, lat, point3.getKey().doubleValue(), point3.getValue().doubleValue());
            if (d3 < 10000) {
                distance3.add(rows.get(i));
            }

            double d4 = GeoUtil.distance(lon, lat, point4.getKey().doubleValue(), point4.getValue().doubleValue());
            if (d4 < 10000) {
                distance4.add(rows.get(i));
            }
        }

        CsvWriter writer1 = CsvUtil.getWriter("/Users/zhangyan/Downloads/distance1.csv", CharsetUtil.CHARSET_UTF_8);
        writer1.write(distance1);

        CsvWriter writer2 = CsvUtil.getWriter("/Users/zhangyan/Downloads/distance2.csv", CharsetUtil.CHARSET_UTF_8);
        writer2.write(distance2);

        CsvWriter writer3 = CsvUtil.getWriter("/Users/zhangyan/Downloads/distance3.csv", CharsetUtil.CHARSET_UTF_8);
        writer3.write(distance3);

        CsvWriter writer4 = CsvUtil.getWriter("/Users/zhangyan/Downloads/distance4.csv", CharsetUtil.CHARSET_UTF_8);
        writer4.write(distance4);
    }

}
